package com.ruoyi.project.system.controller;

import java.util.List;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.TRightorwrong;
import com.ruoyi.project.system.service.ITRightorwrongService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 判断题库Controller
 * 
 * @author ruoyi
 * @date 2020-05-09
 */
@RestController
@RequestMapping("/system/rightorwrong")
public class TRightorwrongController extends BaseController
{
    @Autowired
    private ITRightorwrongService tRightorwrongService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询判断题库列表
     */
    @PreAuthorize("@ss.hasPermi('system:rightorwrong:list')")
    @GetMapping("/list")
    public TableDataInfo list(TRightorwrong tRightorwrong)
    {
        startPage();
        List<TRightorwrong> list = tRightorwrongService.selectTRightorwrongList(tRightorwrong);
        return getDataTable(list);
    }

    /**
     * 导出判断题库列表
     */
    @PreAuthorize("@ss.hasPermi('system:rightorwrong:export')")
    @Log(title = "判断题库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TRightorwrong tRightorwrong)
    {
        List<TRightorwrong> list = tRightorwrongService.selectTRightorwrongList(tRightorwrong);
        ExcelUtil<TRightorwrong> util = new ExcelUtil<TRightorwrong>(TRightorwrong.class);
        return util.exportExcel(list, "rightorwrong");
    }

    /**
     * 获取判断题库详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:rightorwrong:query')")
    @GetMapping(value = "/{rightorwrongId}")
    public AjaxResult getInfo(@PathVariable("rightorwrongId") Integer rightorwrongId)
    {
        return AjaxResult.success(tRightorwrongService.selectTRightorwrongById(rightorwrongId));
    }

    /**
     * 新增判断题库
     */
    @PreAuthorize("@ss.hasPermi('system:rightorwrong:add')")
    @Log(title = "判断题库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TRightorwrong tRightorwrong)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        tRightorwrong.setUserId(user.getUserId().intValue());
        tRightorwrongService.insertTRightorwrong(tRightorwrong);
        System.out.println("tRightorwrong = " + tRightorwrong);
        return toAjax(tRightorwrongService.addMiddle(tRightorwrong.getCourseId(),tRightorwrong.getRightorwrongId()));
    }

    /**
     * 修改判断题库
     */
    @PreAuthorize("@ss.hasPermi('system:rightorwrong:edit')")
    @Log(title = "判断题库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TRightorwrong tRightorwrong)
    {
        return toAjax(tRightorwrongService.updateTRightorwrong(tRightorwrong));
    }

    /**
     * 删除判断题库
     */
    @PreAuthorize("@ss.hasPermi('system:rightorwrong:remove')")
    @Log(title = "判断题库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{rightorwrongIds}")
    public AjaxResult remove(@PathVariable Integer[] rightorwrongIds)
    {
        return toAjax(tRightorwrongService.deleteTRightorwrongByIds(rightorwrongIds));
    }
}
